import scrapy
import re
from ..items import DoubanItem


class DoubandianyingSpider(scrapy.Spider):
    name = 'doubandianying'
    # allowed_domains = ['douban.com']
    start_urls = ['https://movie.douban.com/top250']

    def parse(self, response):
        # print(response.url)
        # print(response.status)
        # print(response.body.decode('utf-8'))
        # title=response.xpath('//ol[@class="grid_view"]//div[@class="hd"]/a/span[1]/text()').extract()
        title = response.css('.hd > a > span:nth-child(1)::text').extract()
        rank = response.xpath('//*[@class="pic"]/em/text()').extract()
        # print(title)
        abstract = response.xpath('//*[@class="inq"]/text()').extract()
        detail_pages = response.xpath('//div[@class="hd"]/a/@href').extract()
        for ind, detail_page in enumerate(detail_pages):
            abstract_detail = abstract[ind]
            title_detail = title[ind]
            rank_detail = rank[ind]
            yield scrapy.Request(detail_page, callback=self.parse_detail, meta={'abstract_detail': abstract_detail,
                                                                                'title_detail': title_detail,
                                                                                'rank_detail': rank_detail})

        next_page = response.xpath('//span[@class="next"]/a/@href').extract_first()
        base_url = 'https://movie.douban.com/top250'
        if next_page:
            yield scrapy.Request(url=base_url + next_page, callback=self.parse)

    def parse_detail(self, response):
        score = response.xpath('//*[@class="ll rating_num"]/text()').extract_first()
        describe = response.xpath('//*[@property="v:summary"]/text()').extract_first()
        abstract_detail = response.meta['abstract_detail']
        title_detail = response.meta['title_detail']
        rank_detail = response.meta['rank_detail']
        print(rank_detail, title_detail, abstract_detail, score, describe)

        item = DoubanItem()
        item['score'] = score
        item['abstract_detail'] = abstract_detail
        item['describe'] = describe
        item['title_detail'] = title_detail
        item['rank_detail'] = rank_detail
        yield item
        # print(item)

        # 正则表达式
        # describe=response.xpath('//*[@property="v:summary"]/text()').extract_first()
        # pattern_num='[0-9]+'
        # number=re.findall(pattern=pattern_num,string=describe)
        # pattern_letter='[a-zA-Z]+'
        # letter=re.findall(pattern=pattern_letter,string=describe)
        # print(number,letter)
